<template>
    <el-container class="home-container">
        <!--页面头部区-->
        <el-header>
            <div>
                <img src="../assets/images/image-20200525191147883.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="loginOut">退出</el-button>
        </el-header>
        <!--主体区-->
        <el-container>
            <!--侧边栏-->
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!--侧边栏菜单区域-->
                <!--开启路由模式-->
                <el-menu
                        router
                        :default-active="active"
                        :collapse="isCollapse"
                        :collapse-transition="false"
                        unique-opened
                        background-color="#333744"
                        text-color="#fff"
                        active-text-color="#409EFF">
                    <!--一级菜单-->
                    <!--每一个菜单都需要有一个独立的index才可以，不然会同时展开-->
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                        <!--一级菜单的模板区域-->
                        <template slot="title">
                            <i :class="iconObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!--二级菜单-->
                        <el-menu-item @click="highNight" :index="'/' + child.path" v-for="child in item.children"
                                      :key="child.id">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{child.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>

            </el-aside>
            <!--主体-->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>

    </el-container>
</template>

<script>
    export default {
        name: "Home",
        data() {
            return {
                //菜单列表
                menuList: [],
                iconObj: {
                    '125': 'iconfont icon-jiaoseguanli',
                    '103': 'iconfont icon-quanxianguanli',
                    '101': 'iconfont icon-shangpinguanli',
                    '102': 'iconfont icon-dingdanguanli-active',
                    '145': 'iconfont icon-shuju'
                },
                //默认为不折叠
                isCollapse: false,
                //高亮标题
                active: '',

            }
        },
        created() {
            this.getMenuList();
            //初始化导航栏高亮显示
            this.active = window.sessionStorage.getItem("active");
        },
        methods: {
            loginOut() {
                window.sessionStorage.clear();
                this.$router.push('/login');
            },
            //获取所有的菜单
            async getMenuList() {
                const {data} = await this.$http.get('menus');
                if (data.meta.status !== 200) return this.$message.error(data.meta.msg);
                this.menuList = data.data;
            },
            //左侧菜单的折叠
            toggleCollapse() {
                this.isCollapse = !this.isCollapse;
            },
            //保存连接的高亮显示
            highNight(e) {
                this.active = e.index;
                window.sessionStorage.setItem("active", e.index);
            }
        }
    }
</script>

<style scoped lang="less">

    .home-container {
        height: 100%;
    }

    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: #fff;
        font-size: 24px;

        > div {
            display: flex;
            height: 100%;
            align-items: center;

            img {
                height: 80%;
                margin: 5px 15px;
            }

            span {
                margin-left: 15px;
            }
        }
    }

    .el-aside {
        background-color: #333744;

        .el-menu {
            border: none;
        }
    }

    .el-main {
        background-color: #eee;
    }

    .iconfont {
        margin-right: 10px;
    }

    .toggle-button {
        background-color: #4a5064;
        font-size: 14px;
        line-height: 24px;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }

</style>
